<!-- 员工管理 -->
<template>
  <div class="app-container">
    <div class="container">
      <el-row>
        <el-form :inline="true" :model="listQuery">
          <el-form-item label="用户名">
            <el-input v-model="listQuery.loginName" placeholder="用户名" clearable  />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="listQuery.status" placeholder="状态" clearable>
              <el-option label="正常" value="0" />
              <el-option label="禁用" value="99" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
            <el-button type="primary" @click="onCreate">新增</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-table :data="list"  v-loading="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row>
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column prop="username" label="登录名" width="245" align="center"></el-table-column>
          <el-table-column prop="role" label="角色" width="245" align="center"></el-table-column>
          <el-table-column label="状态" width="145" align="center">
            <template #default="scope">
              <el-tag>{{scope.row.status === 0 ? '正常' : '禁用'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建日期" width="245" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button
                link
                type="primary"
                icon="el-icon-edit"
                @click="handleEdit(scope.row)"
              >编辑</el-button>
              <el-button link type="primary" icon="el-icon-edit" v-if="scope.row.status === 99" @click="handleStatus(scope.row, '0')">启用</el-button>
              <el-button link type="primary" icon="el-icon-edit" v-if="scope.row.status === 0" @click="handleStatus(scope.row, '99')" >禁用</el-button>
              <!--<el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)" >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination">
          <el-pagination
            layout="sizes, total, prev, pager, next"
            :page-sizes="[10, 20, 30]"
            v-model:page-size="listQuery.rows"
            v-model:currentPage="listQuery.page"
            @current-change="(val) => { fetchData() }"
            @size-change="(val) => { fetchData() }"
            :total="totalRecord"
          ></el-pagination>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import { queryAdminInfo, changeStatus } from '@/api/admin'
import { utilsMixin } from '@/mixin/utils'
export default {
  name: 'employee',
  mixins: [utilsMixin],
  data() {
    return {
      list: null, // 列表数据
      listLoading: true, // 加载状态
      totalRecord: 0, // 数据总条数
      listQuery: { page: 1, rows: 10 },
      listQueryParam: { page: 1 },
      addFormVisible: false, // 新增单窗
      isAdd: false, // 当前是否是新增
      formLoading: false,
      formLabelWidth: '160px',
      Roles: [],
    getViewVisible: false,
    areas: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      queryAdminInfo(this.listQuery).then(response => {
        if (response.success && response.data != null) {
          this.list = response.data.records
          this.totalRecord = response.data.total
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleStatus(row, status) {
      this.$confirm('是否进行此操作？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        changeStatus(row.id, status).then(() => {
          this.fetchData()
        })
      }).catch(() => { })
    },
    onCreate(){
        this.$router.push({ path: '/system/edit/create'})
    },
    handleEdit(row) {
      this.$router.push({ path: '/system/edit/' + row.id })
    },
    // 查询
    onSearch() {
      this.listQuery = Object.assign(this.listQuery, this.listQueryParam) // 合并对象
      this.fetchData()
    },
    // onCreate() {
    //   this.clearForm()
    //   this.addFormVisible = true
    //   this.isAdd = true
    // }

  }
}
</script>
